package com.atguigu.flink04;

import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/2/22
 * 该案例演示了使用Filter实现分流
 */
public class Flink03_SplitStream_Filter {
    public static void main(String[] args) throws Exception {
        //StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<Integer> intDS = env
                .socketTextStream("hadoop102", 8888)
                .map(Integer::parseInt);

        //使用Filter将奇数放到一条流中
        SingleOutputStreamOperator<Integer> ds1 = intDS.filter(num -> num % 2 != 0).setParallelism(2);
        //使用Filter将偶数放到一条流中
        SingleOutputStreamOperator<Integer> ds2 = intDS.filter(num -> num % 2 == 0).setParallelism(3);

        ds1.print("奇数：");
        ds2.printToErr("偶数：");

        env.execute();
    }
}
